package name.sra.png;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.imageio.ImageIO;

public class Editor {
	private Properties config;
	private List<File> files;

	public Editor(Properties config) {
		super();
		this.config = config;
		files = new ArrayList<File>();
		loadFiles();
		performActions();
	}

	private void loadFiles() {
		String filesDir = config.getProperty("files-dir");
		String filesNames = config.getProperty("files");
		String[] filesTab = filesNames.split(",");
		
		for (String fileName : filesTab) {
			File file = new File(filesDir, fileName);
			files.add(file);
		}
	}

	private void performActions() {
		String actions = config.getProperty("actions");
		String[] actionsTab = actions.split(",");
		
		for (String action : actionsTab) {
			performAction(action);
		}
	}

	private void performAction(String action) {
		if ("print".equals(action)) {
			printFiles();
		} else if ("edit".equals(action)) {
			editFiles();
		}
	}

	private void printFiles() {
		Printer printer = new Printer();
		printer.print(files);
		printer.setVisible(true);
	}

	private void editFiles() {
		for (File file : files) {
			editFile(file);
		}
	}
	
	private void editFile(File file) {
		BufferedImage img = ImageUtils.getImage(file);
		for (int i = 0;i < 100;i++) {	
		String rawRGB = Integer.toHexString(img.getRGB(i, 50));
		System.out.print(" " + ImageUtils.correctRGB(rawRGB));
		}
	}
}
